package com.xg.dishes.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xg.dishes.pojo.Dishes;
import com.xg.dishes.service.DishesService;
import com.xg.user.service.UserService;

@Controller
public class DishesController {
	
	@Autowired
	private DishesService dishesService;

	@RequestMapping("/selectAllDishes.do")
	@ResponseBody
	public List<Dishes> selectAllDishes(){
		List<Dishes> list = dishesService.selectAll();
		return list;
}
	
	@RequestMapping("/deleteDishesByDid.do")
	@ResponseBody
	public boolean deleteDishesByDid(Dishes dishes) {
		boolean result =dishesService.deleteDishesByDid(dishes);
		return result;
	}
	
	@RequestMapping("/addDishes.do")
	public String addDishes(@RequestParam("img")MultipartFile f1,Dishes dishes) {
		String filename=f1.getOriginalFilename();
		File file=new File("1811.jpg_wh860.jpg",filename);
		try {
			f1.transferTo(file);
		}catch(IllegalStateException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
		dishes.setDimage("images/dishes/"+filename);
		boolean result=dishesService.addDishes(dishes);
		System.out.println("添加菜品结果："+result);
		return "dishesList";
	}
	
	@RequestMapping("/selectDishesByDid.do")
	@ResponseBody
	public Dishes selectDishesByDid(Dishes dishes) {
		Dishes d=dishesService.selectDishesByDid(dishes);
		return d;
	}
	
	@RequestMapping("/updateDishesPage.do")
	public String updateDishesPage(Dishes dishes,HttpSession session) {
		session.setAttribute("did", dishes);
		return "updateDishes";
	}
	@RequestMapping("/updateDishes.do")
	public String updateDishes(@RequestParam("img")MultipartFile f1,Dishes dishes) {
		String filename=f1.getOriginalFilename();
		File file=new File("1811.jpg_wh860.jpg",filename);
		try {
			f1.transferTo(file);
		}catch(IllegalStateException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
		dishes.setDimage("images/dishes/"+filename);
		boolean result=dishesService.updateDishes(dishes);
		System.out.println("更新菜品结果："+result);
		return "dishesList";
	}
}